﻿using System;
using System.Collections.Generic;

namespace NTrace.VSPackage
{
    public class NTraceConfiguration : ObservableObject
    {
        public const String EtwProviderIdPropertyName = "EtwProviderId";
        public const String EtwProviderVariablePropertyName = "EtwProviderVariable";
        public const String LevelsPropertyName = "Levels";
        public const String FlagsPropertyName = "Flags";

        private Guid etwProviderId;
        private String etwProviderVariable;
        private readonly LevelCollection levels;
        private readonly FlagCollection flags;

        public NTraceConfiguration() 
        {
            this.levels = new LevelCollection();
            this.LoadDefaultLevels();
            this.flags = new FlagCollection();
            this.LoadDefaultFlags();

            this.levels.CollectionChanged += (o, e) => { this.RaisePropertyChanged(LevelsPropertyName); };
            ((System.ComponentModel.INotifyPropertyChanged)this.levels).PropertyChanged += (o, e) => { this.RaisePropertyChanged(LevelsPropertyName); };
            this.flags.CollectionChanged += (o, e) => { this.RaisePropertyChanged(FlagsPropertyName); };
            ((System.ComponentModel.INotifyPropertyChanged)this.flags).PropertyChanged += (o, e) => { this.RaisePropertyChanged(FlagsPropertyName); };
        }

        private void LoadDefaultLevels()
        {
            this.levels.Add(new LevelEntry("Fatal", 1));
            this.levels.Add(new LevelEntry("Error", 2));
            this.levels.Add(new LevelEntry("Warning", 3));
            this.levels.Add(new LevelEntry("Information", 4));
            this.levels.Add(new LevelEntry("Verbose", 5));
            this.levels.Add(new LevelEntry("Noise", 6));
        }

        private void LoadDefaultFlags()
        {
            this.flags.Add(new FlagEntry("Component", 0));
            this.flags.Add(new FlagEntry("Protocol", 1));
            this.flags.Add(new FlagEntry("Connection", 2));
            this.flags.Add(new FlagEntry("Security", 3));
            this.flags.Add(new FlagEntry("Diagnostic", 4));
            this.flags.Add(new FlagEntry("Authentication", 5));
            this.flags.Add(new FlagEntry("Parsing", 6));
            this.flags.Add(new FlagEntry("Network", 7));
            this.flags.Add(new FlagEntry("StackTracing", 8));
        }
        
        public String EtwProviderId 
        {
            get
            {
                return this.etwProviderId.ToString("B");
            }
            set
            {
                Guid guidValue = new Guid(value);
                if (this.etwProviderId == guidValue) return;
                this.etwProviderId = guidValue;
                this.RaisePropertyChanged(NTraceConfiguration.EtwProviderIdPropertyName);       
            }
        }

        public String EtwProviderVariable 
        {
            get
            {
                return this.etwProviderVariable;
            }
            set
            {
                if (this.etwProviderVariable == value) return;
                this.etwProviderVariable = value;
                this.RaisePropertyChanged(NTraceConfiguration.EtwProviderVariablePropertyName);       
            }
        }

        public LevelCollection Levels
        {
            get
            {
                return this.levels;
            }
        }

        public FlagCollection Flags
        {
            get
            {
                return this.flags;
            }
        }
    }
}
